package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;

    @Autowired
    private BusinessMapper businessMapper;

    //查询线索列表
    @Override
    public PageResult<Clue> listClues(ClueDto clueDto) {
       Page<Clue> cluePage = this.clueMapper.listClues(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
       return new PageResult<Clue>(cluePage.getTotal(), cluePage.getRecords());
    }
    //根据id查询线索
    @Override
    public Clue getClueById(Integer id) {
        return this.clueMapper.getClueById(id);
    }

    //线索跟进
    @Transactional(rollbackFor = Exception.class)//事务控制
    @Override
    public void trackClue(Clue clue){
        //更新线索状态为跟进中
        clue.setStatus(3);
        clue.setUpdateTime(LocalDateTime.now());
        this.clueMapper.updateById(clue);


        //添加跟进记录
        ClueTrackRecord trackRecord = new ClueTrackRecord();
        trackRecord.setClueId(clue.getId());
        trackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        trackRecord.setSubject(clue.getSubject());
        trackRecord.setLevel(clue.getLevel());
        trackRecord.setRecord(clue.getRecord());
        trackRecord.setNextTime(clue.getNextTime());
        trackRecord.setType(1);
        trackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(trackRecord);
    }

    @Override
    public void updateClueTrackRecord(MarkFalseClueDto markFalseClueDto) {
        //添加伪线索信息
        ClueTrackRecord trackRecord = new ClueTrackRecord();
        trackRecord.setClueId(markFalseClueDto.getClueId()); // 设置线索ID
        trackRecord.setUserId(CurrentUserHoler.getCurrentUser()); // 设置当前用户ID
        trackRecord.setType(0); // 跟进类型为0，表示伪线索
        trackRecord.setFalseReason(markFalseClueDto.getReason()); // 伪线索原因
        trackRecord.setRecord(markFalseClueDto.getRemark()); // 备注说明
        trackRecord.setCreateTime(LocalDateTime.now()); // 创建时间

        clueTrackRecordMapper.insert(trackRecord);
    }

    @Override
    public void toBusiness(Integer id) {
        //根据id更新线索转为商机
        Clue clue =clueMapper.selectById(id);
        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);


        //添加商机信息(根据线索组装商机信息)
        Business business = new Business();
        BeanUtils.copyProperties(clue,business);
        business.setId(null);
        business.setUserId(null);
        business.setStatus(1);
        business.setClueId(id);
        business.setNextTime(null);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());

        businessMapper.insert(business);
    }

    @Override
    public PageResult<Clue> listCluesPool(ClueDto clueDto) {
        Page<Clue> page= this.clueMapper.listCluesPool(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new PageResult<Clue>(page.getTotal(), page.getRecords());
    }
}
